local function onmax(self, max)
    self.inst.replica.obrsword_power:SetMax(max)
end

local function oncurrent(self, current)
    self.inst.replica.obrsword_power:SetCurrent(current)
end

local obrsword_power = Class(function(self, inst)

    self.inst = inst
    self.extrapower = 0
    self.max = 100
    self.current = 100
    self.obrsword_power_damage = 25
    self.sword_talk_cd = 0

    self.level1 = 50
    self.level2 = 75
    self.level3 = 3000
    self.level4 = 5000
    self.dragonfly = false
    self.moose = false
    self.antlion = false
    self.opalpreciousgem = false
    -- self.task=DoPeriodicTask(1, function(target)
    -- self.current =self.current+1+0.1*(51-math.abs(self.current-50))end

end, nil, {
    max = onmax,
    current = oncurrent
})

function obrsword_power:Getobrsword_power()
    return self.current
end
--  ThePlayer.components.obrsword_power:reviseelement()
    

function obrsword_power:OnSave() -- 保存
    return {
        current = self.current,
        damage = self.obrsword_power_damage,
        drg = self.dragonfly,
        moose = self.moose,
        ant = self.antlion,
        op = self.opalpreciousgem,
    } -- 保存当前值
end

function obrsword_power:OnLoad(data) -- 加载
    if data.current ~= nil then
        self.current = data.current
    end

    if data.damage ~= nil then
        self.obrsword_power_damage = data.damage
    end

    if data.drg ~= nil then
        self.dragonfly = data.drg
    end

    if data.moose ~= nil then
        self.moose = data.moose
    end

    if data.ant ~= nil then
        self.antlion = data.ant
    end

    if data.op ~= nil then
        self.opalpreciousgem = data.op
    end

    self:DoDelta(0)
end

function obrsword_power:reviseelement()
    self.dragonfly = true
    self.moose = true
    self.antlion = true
end
function obrsword_power:reviseelementfire()
    self.dragonfly = true
 
end
--  ThePlayer.components.obrsword_power:reviseelementfire()
function obrsword_power:SetMax(amount) -- 设置最大值
    self.max = amount
    -- self.current = self.max
    self.inst:PushEvent("obrsword_powerdirty", {self.max})
end

function obrsword_power:DoDelta(delta) -- 改变的函数

    local function clamp(value, min, max)
        if value < min then
            return min
        elseif value > max then
            return max
        else
            return value
        end
    end
    local old = self.current
    self.current = math.clamp(self.current + delta, 0, self.max)
    self.obrsword_power_damage = 0.6 * clamp(self.current - 50, 0, 250)+self.extrapower*0.2
    if self.inst.components.inventory.equipslots.hands ~= nil then

        local weapon = self.inst.components.inventory.equipslots.hands
        if weapon:HasTag("obrsword") then
            if not weapon.components.planardamage then
                local planardamage = self.inst:AddComponent("planardamage")
                planardamage:SetBaseDamage(self.obrsword_power_damage)
            else
                weapon.components.planardamage:SetBaseDamage(self.obrsword_power_damage)

            end
        end
    end
    -- 其实改变的时候事件和需要传的参数都是随自己看需求写的
    self.inst:PushEvent("obrsword_powerdelta", {
        oldpercent = old / self.max,
        newpercent = self.current / self.max
    })
    if self.sword_talk_cd ~= 0 then
        self.sword_talk_cd = self.sword_talk_cd - 1
    end
    if self.inst.components.inventory.equipslots.hands == nil then
        return
    end

    local weapon = self.inst.components.inventory.equipslots.hands
    if not weapon:HasTag("obrsword") then
        return
    end

    if self.current == 0 then
        self.inst.components.talker:Say("不妙，光明需要援助")
    elseif self.current >= 22 and self.current < 25 then
        self.inst.components.talker:Say("光之衰弱")
    elseif self.current >= 50 and self.current < 55 then
        self.inst.components.talker:Say("银河之光，召唤着我！")
    elseif self.current >= 75 and self.current < 80 then
        self.inst.components.talker:Say("我将化身光明，击退黑暗！")
    elseif self.current == 100 and self.sword_talk_cd == 0 then
        self.sword_talk_cd = 80
        self.inst.components.talker:Say("我叫欧布，欧布奥特曼，我将照亮黑暗，击退邪恶！")
    end

end

function obrsword_power:GetPercent() -- 获取百分比
    return self.current / self.max
end
function obrsword_power:Getallpower() -- 获取百分比

    return  self.dragonfly and self.moose and self.antlion and self.antlion 
   
end
function obrsword_power:SetPercent(p) -- 设置百分比
    local old = self.current
    self.current = p * self.max
    self.inst:PushEvent("obrsword_powerdelta", {
        oldpercent = old / self.max,
        newpercent = p
    })
end
function obrsword_power:checkConditions()
    -- 检查所有条件是否都为true
    if TUNING.ANTLION and TUNING.MOOSE and TUNING.DRAGONFLY  then
        return true
    end
end
function obrsword_power:start_obrsword_power(inst)
    -- 战斗状态下能量少量自然恢复，脱战时自然衰减
    inst.powertask = inst:DoPeriodicTask(1, function(inst)

        local function testfn(inst)
            if inst:HasTag("monster") or inst:HasTag("shadow")  or inst:HasTag("epic") then
                return true
            end
        end
        local mouse_x, mouse_y, mouse_z = inst.Transform:GetWorldPosition()
        local current = inst.components.obrsword_power.current
        local ents = TheSim:FindEntities(mouse_x, mouse_y, mouse_z, 32, {'locomotor'},
            {'INLIMBO', 'inst', 'companion', 'wall', "player"})
        for _, v in pairs(ents or {}) do
            if v and v:IsValid() and v.components and v.components.health and not v.components.health:IsDead() then
                if testfn(v) then
                    local obr = 0.2 + 0.01 * math.abs(50 - math.abs(current - 50))
                    inst.components.obrsword_power:DoDelta(obr)

                else
                    if inst.components.obrsword_power.opalpreciousgem then return end
local powerlimit = 0.5*inst.components.obrsword_power.max+10+0.8*inst.components.obrsword_power.extrapower  or 70
                    if inst.components.obrsword_power.current >= powerlimit then
                        inst.components.obrsword_power:DoDelta(-0.3)
                    end
                end
            end
        end
    end)
end

return obrsword_power
